package com.example.zzl.model.bean.SearchFragment;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Environment;
import android.util.Log;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.example.zzl.app.MyApplication;
import com.example.zzl.view.activity.SearchActivity.SearchResultActivity;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;

/**
 * Created by zhang on 2017/2/8.
 */

public class LoadData {
    private Data data;

    public LoadData(Data data,String url,String type) {
        this.data = data;
        if(type.equals("Search")){
            loadData1(url);
        }else {
            loadData(url, type);
        }

    }

    private void loadData1(String url) {
        final StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                    SearchResultData resultData = new Gson().fromJson(response,SearchResultData.class);
                    ArrayList<SearchResultData.ResultBean.InfoListBean> info = new ArrayList<>();
                    ArrayList<SearchResultData.ResultBean.FilterListBean> filter = new ArrayList<>();
                    ArrayList<SearchResultData.ResultBean.FilterListBean.ListHotTagBean> list1,list2,list3;
                    info = (ArrayList<SearchResultData.ResultBean.InfoListBean>) resultData.getResult().getInfoList();
                    filter = (ArrayList<SearchResultData.ResultBean.FilterListBean>) resultData.getResult().getFilterList();
//                    if(resultData==null){
//                        SearchResultActivity.handler.sendEmptyMessage(0x1111);
//                    }else {
                        //Log.e("data",resultData.getResult().getFilterList().get(0).getListHotTag().size()+"QQQ");
                        data.sendInfoData(info);
                        data.sendFilterData(filter);
                    //}

            }
        },new Response.ErrorListener(){
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("data",error.toString());
                SearchResultActivity.handler.sendEmptyMessage(0x1121);
            }
        });
        request.setRetryPolicy( new DefaultRetryPolicy( 10000,//默认超时时间，应设置一个稍微大点儿的，例如本处的500000
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,//默认最大尝试次数
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT ) );
        MyApplication.queue.add(request);
    }

    public void loadData(String url, final String type){
        //String url = "http://mws.biyabi.com/WebService.asmx/GetHotTagGroupJson";
        final StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if(type.equals("Cif")) {
                    ArrayList list = new Gson().fromJson(response.toString(), ArrayList.class);
                    Brand brand = new Gson().fromJson(new Gson().toJson(list.get(0)), Brand.class);
                    Cat cat = new Gson().fromJson(new Gson().toJson(list.get(1)), Cat.class);
                    Mall mall = new Gson().fromJson(new Gson().toJson(list.get(2)), Mall.class);
                    data.sendBrand(brand);
                    data.sendCat(cat);
                    data.sendMall(mall);
                    //Log.e("data",cat.getStrGroupName());
                }else if(type.equals("Brand")){
                    SearchBrand sb = new Gson().fromJson(response,SearchBrand.class);
                    ArrayList<SearchBrand.ResultBeanX> list = new ArrayList<>();
                    list = (ArrayList<SearchBrand.ResultBeanX>) sb.getResult();
                    data.snedSearchResult(list);
                }else if(type.equals("Search")){
                    Log.e("data",response);

                    //saveFile(response.toString());
                    SearchResultData resultData = new Gson().fromJson(response,SearchResultData.class);
                    ArrayList<SearchResultData.ResultBean.InfoListBean> info = new ArrayList<>();
                    ArrayList<SearchResultData.ResultBean.FilterListBean> filter = new ArrayList<>();
                    ArrayList<SearchResultData.ResultBean.FilterListBean.ListHotTagBean> list1,list2,list3;
                    info = (ArrayList<SearchResultData.ResultBean.InfoListBean>) resultData.getResult().getInfoList();
                    filter = (ArrayList<SearchResultData.ResultBean.FilterListBean>) resultData.getResult().getFilterList();
                    //Log.e("data",resultData.getResult().getFilterList().get(0).getListHotTag().size()+"QQQ");
                    data.sendInfoData(info);
                    data.sendFilterData(filter);
                }

            }
        },new Response.ErrorListener(){
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("data",error.toString());
            }
        });
        request.setRetryPolicy( new DefaultRetryPolicy( 10000,//默认超时时间，应设置一个稍微大点儿的，例如本处的500000
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,//默认最大尝试次数
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT ) );
        //MyApplication.queue.add(request);
        MyApplication.queue.add(request);
    }

    public static void saveFile(String str) {
        String filePath = null;
        boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        if (hasSDCard) { // SD卡根目录的hello.text
            filePath = Environment.getExternalStorageDirectory().toString() + File.separator + "hello.txt";
        } else  // 系统下载缓存根目录的hello.text
            filePath = Environment.getDownloadCacheDirectory().toString() + File.separator + "hello.txt";

        try {
            File file = new File(filePath);
            if (!file.exists()) {
                File dir = new File(file.getParent());
                dir.mkdirs();
                file.createNewFile();
            }
            FileOutputStream outStream = new FileOutputStream(file);
            outStream.write(str.getBytes());
            outStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




}
